/*
 * Created on 26-Apr-05
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.one.stone.soup.grfxML.plugin.system;

import java.awt.Component;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import org.one.stone.soup.grfx.ImageFactory;
import org.one.stone.soup.grfxML.DataImage;
import org.one.stone.soup.grfxML.DataNode;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.TagModifier;
import org.one.stone.soup.stringhelper.FileNameHelper;

public class ImageDropHandler implements DropHandler {

	public ImageDropHandler()
	{
	}

	/* (non-Javadoc)
	 * @see org.one.stone.soup.grfxML.plugin.system.DropHandler#matches(java.lang.String)
	 */
	public boolean matches(String name,int x,int y) {
		String fileExtension = FileNameHelper.getExt(name).toLowerCase();

		if(
			fileExtension.equals("gif") ||
			fileExtension.equals("jpg") ||
			fileExtension.equals("png")
		)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see org.one.stone.soup.grfxML.plugin.system.DropHandler#handle(java.lang.String)
	 */
	public boolean handle(String name,GrfxMLEngine engine,int x,int y) {

		try{
			if(!name.substring(0,5).equals("http:"))
			{
				name = new File(name).toURL().toString();
			}
			String fileExtension = FileNameHelper.getExt(name).toLowerCase();

			if(
				fileExtension.equals("gif") ||
				fileExtension.equals("jpg") ||
				fileExtension.equals("png")
			)
			{

				String[] layers = new String[]{"Background","Foreground","Overlay"};

				int layer = JOptionPane.showOptionDialog(
					(Component)engine.getView(),
					"Select layer",
					"Add an Image",
					JOptionPane.OK_CANCEL_OPTION,
					JOptionPane.QUESTION_MESSAGE,
					new ImageIcon(ImageFactory.loadImage("jar://resources/giraffe.png")),
					(Object[])layers,
					layers[1]
				);
				if(layer<0)
				{
					return false;
				}

				String layerName = layers[layer].toLowerCase();

				DataNode layerNode = (DataNode)engine.getAPI().getField(layerName);

				if(layerNode==null)
				{
					switch(layer)
					{
						case 0:
							layerNode = engine.getAPI().getBase().getBACKGROUND();
							break;
						case 1:
							layerNode = engine.getAPI().getBase().getROOT();
							break;
						case 2:
							layerNode = engine.getAPI().getBase().getGUI();
							break;
					}
				}

				x = x-layerNode.getTranslation().getPositionView().getValueX();
				y = y-layerNode.getTranslation().getPositionView().getValueY();

				if(layer<2)
				{
					layerNode.getShape().setEmpty();

					DataImage image = new DataImage();
					image.getOffset().setValueX(x,this);
					image.getOffset().setValueY(y,this);
					image.setFile( new DataString( TagModifier.STRING_TYPE_FILE,name ) );
					layerNode.getShape().add(image,this);
				}
				else
				{
					DataNode newNode = new DataNode(engine);
					newNode.getBehaviour().setSelectable(new DataState(true));
					newNode.getBehaviour().setDraggable(new DataState(true));

					newNode.getTranslation().getPosition().setValueX(x,this);
					newNode.getTranslation().getPosition().setValueY(y,this);

					layerNode.getChildren().add(newNode,this);
					layerNode = newNode;

					DataImage image = new DataImage();
					image.setFile( new DataString( TagModifier.STRING_TYPE_FILE,name ) );

					layerNode.getShape().add(image,this);
				}

				engine.getAPI().commit();
				if(layer==0)
				{
					engine.getAPI().refreshBackground();
				}
			}

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return true;
	}
}
